;(function (options) {
	function CalendarSelector(options) {
		this._params = options;
		this.calendarCore = new $.util.CalendarCore();
		this.calendarCore.setDatePrototype(options.viewDate ? options.viewDate : new Date());
		this.selectResult = {};
		this.eventsok = false;
		this.render();
	};
	
	var Prototype = CalendarSelector.prototype;
	
	Prototype.isHoliday = function (year, month, date, day) {
		var hlLen = this.holidayList.length;
		var selected = this.selectResult[year + "-" + month + "-" + date];
		if(selected)
			return selected;
		for(var i=0;i<hlLen;++i)
		{
			var info = this.holidayList[i];
			if(info.year == year && info.month == month && info.day == date)
				return info.holiday;
		}
		if(day == 6 || day == 0)
			return true;
		return false;
	};
	
	Prototype.getSelected = function () {
		return this.selectResult;
	};
	
	function parseHolidaList(serverHL) {
		if(!serverHL)
			return [];
		var length = serverHL.length;
		var finalList = [];
		for(var i=0;i<length;++i)
		{
			finalList.push({
				holiday: serverHL[i].holidaySn == 1 || serverHL[i].weekSn == 0,
				year: serverHL[i].vacationYear,
				month: serverHL[i].vacationMonth,
				day: serverHL[i].vacationDay
			});
		}
		return finalList;
	};
	
	Prototype.render = function (holidaylist) {
		var host = this;
		var parent = this._params.parent;
		var params = {};
		var requestMonth = this.calendarCore.getMonth();
		params[ACTIONS.HOLIDAY.HOLIDAY_LIST.params.YEAR] = this.calendarCore.getYear();
		params[ACTIONS.HOLIDAY.HOLIDAY_LIST.params.MONTH] = requestMonth >= 10 ? requestMonth : "0" + requestMonth;
		$.util.Data.request({
			url: ACTIONS.HOLIDAY.HOLIDAY_LIST.url,
			dataType: "json",
			data: params,
			success: function (data) {
				host.holidayList = parseHolidaList(data.holidayList);
				oacore.UI.render(host._params.ui, {me: host, cs: host.calendarCore}, parent.empty(), function () {
					if(host.eventsok == false)
					{
						parent.find("a.month-control").live("click", function () {
							var month = this.id.split("-")[1];
							host.calendarCore.setMonth(month);
							host.render();
						});
						parent.find("button.year-sb-pre").live("click", function () {
							host.calendarCore.setYear(host.calendarCore.getYear() - 1);
							host.render();
							return false;
						});
						parent.find("button.year-sb-next").live("click", function () {
							host.calendarCore.setYear(host.calendarCore.getYear() + 1);
							host.render();
							return false;
						});
						parent.find("span.sd-year").live("dblclick", function () {
							host.calendarCore.setYear(host.calendarCore.getNowYear());
							host.render();
						});
						parent.find("input.holiday-select").live("click", function () {
							var input = $(this);
							host.selectResult[input.attr("id").substr(5)] = input.attr("checked");
						});
						host.eventsok = true;
					}
				});
			}
		});
	};
	
	window.CalendarSelector = CalendarSelector;
})();